package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfVideocallInfo;
import com.ocom.common.entity.card.EntityXfVideocallNo;
import com.ocom.common.entity.card.EntityXfWalletInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.videocall.request.SaveAccreditRequest;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfVideocallInfo;
import com.ocom.payconfig.dao.IDaoXfVideocallNo;
import com.ocom.payconfig.service.XfVideocallInfoService;
import com.ocom.payconfig.service.XfVideocallNoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class XfVideocallNoServiceImpl extends ServiceImpl<IDaoXfVideocallNo, EntityXfVideocallNo> implements XfVideocallNoService {

    @Autowired
    private IDaoXfVideocallNo dao;


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfVideocallNo> getByComIdAndPIdAndOpenid(Long companyId, Long pId, String openid) {
        QueryWrapper<EntityXfVideocallNo> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        if (null != pId) {
            queryWrapper.eq("p_id", pId);
        }
        if (StringUtils.isNotBlank(openid)) {
            queryWrapper.eq("openid", openid);
        }
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean saveAccreditInfo(Long companyId, SaveAccreditRequest request) {

        QueryWrapper<EntityXfVideocallNo> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("group_id", request.getGroupId());
        queryWrapper.eq("p_id", request.getPId());
        queryWrapper.eq("openid", request.getOpenid());

        EntityXfVideocallNo one = this.getOne(queryWrapper);

        if (null == one) {
            one = new EntityXfVideocallNo();
            one.setComId(companyId);
            one.setGroupId(request.getGroupId());
            one.setPId(request.getPId());
            one.setOpenid(request.getOpenid());
        }
        one.setStatus(request.getStatus());

        return this.saveOrUpdate(one);
    }
}
